package com.lb.shopguide.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lb.shopguide.R;

/**
 * Created by kang on BOTTOM_RecBOTTOM_HOME17/5/BOTTOM_MINEBOTTOM_HOME.
 */

public class LbBottomBar extends LinearLayout implements View.OnClickListener {

    public static final int BOTTOM_HOME = 0;
    public static final int BOTTOM_MESSAGE = 1;
    public static final int BOTTOM_REC = 2;
    public static final int BOTTOM_MINE = 3;

    private LinearLayout layoutHome;
    private LinearLayout layoutMember;
    private LinearLayout layoutRec;
    private LinearLayout layoutMine;

    private ImageView ivHome;
    private ImageView ivMember;
    private ImageView ivRec;
    private ImageView ivMine;

    private TextView tvHome;
    private TextView tvMember;
    private TextView tvRec;
    private TextView tvMine;

    private int pos;
    private int colorSelected = Color.rgb(18, 18, 18);
    private int colorUnSelected = Color.rgb(172, 172, 172);

    public LbBottomBar(Context context) {
        super(context);
    }

    public LbBottomBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_lb_bottombar, this);
        tvHome = findViewById(R.id.tv_merchant);
        tvMember = findViewById(R.id.tv_secretary);
        tvRec = findViewById(R.id.tv_rec);
        tvMine = findViewById(R.id.tv_mine);

        ivHome = findViewById(R.id.iv_merchant);
        ivMember = findViewById(R.id.iv_secretary);
        ivRec = findViewById(R.id.iv_rec);
        ivMine = findViewById(R.id.iv_mine);

        layoutHome = findViewById(R.id.layout_merchant);
        layoutMember = findViewById(R.id.layout_message);
        layoutRec = findViewById(R.id.layout_rec);
        layoutMine = findViewById(R.id.layout_mine);

        layoutHome.setOnClickListener(this);
        layoutMember.setOnClickListener(this);
        layoutRec.setOnClickListener(this);
        layoutMine.setOnClickListener(this);

        setChecked(BOTTOM_HOME);
    }

    public LbBottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setChecked(int pos) {
        clearCheck();
        switch (pos) {
            case BOTTOM_HOME:
                ivHome.setSelected(true);
                tvHome.setTextColor(colorSelected);
                break;
            case BOTTOM_MESSAGE:
                ivMember.setSelected(true);
                tvMember.setTextColor(colorSelected);
                break;
            case BOTTOM_REC:
                ivRec.setSelected(true);
                tvRec.setTextColor(colorSelected);
                break;
            case BOTTOM_MINE:
                ivMine.setSelected(true);
                tvMine.setTextColor(colorSelected);
                break;
        }
        this.pos = pos;
    }

    private void clearCheck() {
        ivHome.setSelected(false);
        tvHome.setTextColor(colorUnSelected);

        ivMember.setSelected(false);
        tvMember.setTextColor(colorUnSelected);

        ivRec.setSelected(false);
        tvRec.setTextColor(colorUnSelected);

        ivMine.setSelected(false);
        tvMine.setTextColor(colorUnSelected);
    }

    @Override
    public void onClick(View v) {
        if (mOnTabSelectedListener != null) {
            switch (v.getId()) {
                case R.id.layout_merchant:
                    if (pos != BOTTOM_HOME) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_HOME, pos);
                        setChecked(BOTTOM_HOME);
                    }
                    break;
                case R.id.layout_message:
                    if (pos != BOTTOM_MESSAGE) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_MESSAGE, pos);
                        setChecked(BOTTOM_MESSAGE);
                    }
                    break;
                case R.id.layout_rec:
                    if (pos != BOTTOM_REC) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_REC, pos);
                        setChecked(BOTTOM_REC);
                    }
                    break;
                case R.id.layout_mine:
                    if (pos != BOTTOM_MINE) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_MINE, pos);
                        setChecked(BOTTOM_MINE);
                    }
                    break;
            }
        }
    }

    public void performClick(int newpos){
        mOnTabSelectedListener.onTabSelected(newpos, pos);
        setChecked(newpos);
    }

    public interface OnTabSelectedListener {
        void onTabSelected(int pos, int prePos);
    }

    public void setOnTabSelectedListener(OnTabSelectedListener listener) {
        mOnTabSelectedListener = listener;
    }

    private OnTabSelectedListener mOnTabSelectedListener;
}
